package com.hellogood.yaohuo.server.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.db.ConnectionPool;
import com.hellogood.yaohuo.server.db.DBHelper;
import com.hellogood.yaohuo.shared.entity.Vendor;

public class VendorDAO extends DBHelper {
	Logger log = Logger.getLogger(this.getClass());
	String hxDbName = ConnectionPool.hxDbName;

	@Override
	public List<?> initializeObject(ResultSet rs) throws SQLException {
		List<Vendor> list = new ArrayList<Vendor>();
		if (rs != null) {
			while (rs.next()) {
				Vendor vendor = new Vendor();
				vendor.setVendorCode(rs.getString("vendorCode"));
				vendor.setVendorName(rs.getString("vendorName"));
				list.add(vendor);
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Vendor> getVendorList() {
		String sql = "SELECT vendorCode,vendorName FROM " + hxDbName + "..Vendor ORDER BY vendorCode";
		List<?> list = super.query(sql);
		return (List<Vendor>) list;
	}

	public Vendor getVendorByCode(String code) {
		String sql = "SELECT vendorCode,vendorName FROM " + hxDbName + "..Vendor WHERE vendorCode = '" + code + "'";
		List<?> list = super.query(sql);
		if (list != null && list.size() > 0) {
			return (Vendor) list.get(0);
		}
		return null;
	}
}
